﻿'Copyright (c) 2009 Grailtek

'Permission is hereby granted, free of charge, to any person
'obtaining a copy of this software and associated documentation
'files (the "Software"), to deal in the Software without
'restriction, including without limitation the rights to use,
'copy, modify, merge, publish, distribute, sublicense, and/or sell
'copies of the Software, and to permit persons to whom the
'Software is furnished to do so, subject to the following
'conditions:

'The above copyright notice and this permission notice shall be
'included in all copies or substantial portions of the Software.

'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
'OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
'HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
'WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
'FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
'OTHER DEALINGS IN THE SOFTWARE.

Imports System.Aspect

Namespace Controls

    Public Class VerbToolStrip
        Inherits ToolStrip

        Public Sub New()
            MyBase.New()
        End Sub

        Private Buttons As New List(Of VerbButton)

        Private Class VerbButton
            Inherits ToolStripButton
            Public Sub New()
                Me.DisplayStyle = ToolStripItemDisplayStyle.Text
                Me.AutoToolTip = False
            End Sub
            Private _Verb As Verb
            Public Property Verb() As Verb
                Get
                    Return _Verb
                End Get
                Set(ByVal value As Verb)
                    _Verb = value
                    If value Is Nothing Then
                        Me.Text = Nothing
                        Me.Visible = False
                    Else
                        Me.Text = value.Verb.UIName
                        Me.Visible = True
                    End If
                End Set
            End Property

            Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
                If Not Verb Is Nothing Then
                    Verb.Invoke(Nothing)
                End If
                MyBase.OnClick(e)
            End Sub

        End Class

        Public Sub SetVerbs(ByVal verbs() As Verb)
            For Each v As VerbButton In Buttons
                v.Verb = Nothing
            Next
            Dim verbCount As Integer = verbs.Count
            Dim newButtonsRequired As Integer = verbCount - Buttons.Count
            If newButtonsRequired > 0 Then
                For i As Integer = 1 To newButtonsRequired
                    Dim btn As New VerbButton
                    Buttons.Add(btn)
                    Me.Items.Add(btn)
                Next
            End If
            For i As Integer = 0 To verbCount - 1
                Buttons(i).Verb = verbs(i)
            Next
        End Sub

    End Class

End Namespace

